package com.mocha.bass.test.mianzhu;

import com.alibaba.fastjson.JSON;
import com.hyzh.latte.NezhaApplication;
import com.hyzh.latte.vehicle.dmo.PointBO;
import com.hyzh.latte.vehicle.dmo.entity.PointPGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePointPGEntity;
import com.hyzh.latte.vehicle.repository.CameraPGRepository;
import com.hyzh.latte.vehicle.repository.PointPGRepository;
import com.hyzh.latte.vehicle.repository.PolePGRepository;
import com.hyzh.latte.vehicle.repository.PolePointPGRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/4/25 16:21
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NezhaApplication.class)
@Slf4j
public class UpatePointHeight {

    @Autowired
    private PointPGRepository pointPGRepository;
    @Autowired
    private PolePointPGRepository polePointPGRepository;
    @Autowired
    private CameraPGRepository cameraPGRepository;
    @Autowired
    private PolePGRepository polePGRepository;

    @Test
    public void pole() {
        List<PointPGEntity> pointList = pointPGRepository.list();
        Map<String, PointPGEntity> pointMap = pointList.stream().collect(Collectors.toMap(PointPGEntity::getPointNo, Function.identity()));
        List<PolePointPGEntity> list = polePointPGRepository.list();

        for (PolePointPGEntity entity : list){
            String[] ps = entity.getPoints().split(",");
            List<PointBO> pointBOList = new ArrayList<>(ps.length);
            for (String p : ps){
                PointPGEntity point = pointMap.get(p);
                if (null == point){
                    log.error("**********************Point {} is null", p);
                }else {
                    PointBO pointBO = new PointBO();
                    pointBO.setLon(point.getLon());
                    pointBO.setLat(point.getLat());
                    pointBO.setH(point.getHeight());
                    pointBO.setT(1);
                    pointBO.setDistance(30);
                    pointBOList.add(pointBO);
                }
            }

            PolePointPGEntity upd = new PolePointPGEntity();
            upd.setId(entity.getId());
            upd.setPointJson(JSON.toJSONString(pointBOList));


            polePointPGRepository.updateById(upd);

        }



    }
}
